//
//  DEMOMenuViewController.m
//  RESideMenuExample
//
//  Created by Roman Efimov on 10/10/13.
//  Copyright (c) 2013 Roman Efimov. All rights reserved.
//

#import "DEMOLeftMenuViewController.h"
#import "CollectiTVC.h"

@interface DEMOLeftMenuViewController ()<UIAlertViewDelegate>

@property (strong, readwrite, nonatomic) UITableView *tableView;

@end

@implementation DEMOLeftMenuViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.titlesArray= @[@"首页", @"收藏", @"清除缓存",@"关于我们"];
    
    
    self.tableView = ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - 54 * 5) / 2.0f+50, self.view.frame.size.width, 54 * 5) style:UITableViewStylePlain];
        tableView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.opaque = NO;
       tableView.backgroundColor = [UIColor clearColor];
        tableView.backgroundView = nil;
        tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        tableView.bounces = NO;
        tableView;
    
    });
    
    self.tableView.tableFooterView=[[UIView alloc] init];
    self.tableView.separatorColor=[UIColor colorWithWhite:1.000 alpha:0.200];
    [self.view addSubview:self.tableView];
}

#pragma mark -
#pragma mark UITableView Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:
        {
            RootTabBarC *rootTBC =[RootTabBarC sharedInstence];
            UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:rootTBC];
            rootTBC.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:40/255.0 green:65/255.0 blue:92/255.0 alpha:1.0];
            navi.navigationBar.translucent =NO;
            [self.sideMenuViewController setContentViewController:navi];
            [self.sideMenuViewController hideMenuViewController];

        }
            break;
        case 1:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[CollectiTVC alloc] init]]
                                                         animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 2:
           
            [self didClick];

            break;
        case 3:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[StatementViewController alloc] init]]
                                                         animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
        default:
            break;
    }
}
- (void)showAlertController:(NSNumber*)number{
    long long cachesSize = [number longLongValue];
    __weak typeof(self) weakSelf = self;
    
    
    
    //-------版本<8.0--------
    CGFloat version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version>=8.0) {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"您的内存为:%lld MB,确定要清理吗?",cachesSize]         preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        
        [GetFileSize showAllFileWithPath:^{
        UIAlertController *allertComplete = [UIAlertController alertControllerWithTitle:@"" message:@"清理成功!" preferredStyle:UIAlertControllerStyleAlert];
                
        [weakSelf presentViewController:allertComplete animated:YES completion:^{
                    [weakSelf performSelector:@selector(dismisAllertComplete:) withObject:allertComplete afterDelay:0.5];
                }];
           }];
     }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    }];
    [alert addAction:action];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"您的内存为:%lld MB,确定要清理吗?",cachesSize] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [alert show];
    }
}

//版本小于8.0--使用alertView-----
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 1:
            [alertView dismissWithClickedButtonIndex:0 animated:YES];
            break;
        case 0:
        {
            
            [GetFileSize showAllFileWithPath:^{
                [self performSelectorOnMainThread:@selector(presentAlertView) withObject:nil waitUntilDone:NO];
            }];
        }
            break;
        default:
            break;
    }
}

- (void)presentAlertView{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"清理成功" delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alert show];
    
    [self performSelector:@selector(dismissAlertView:) withObject:alert afterDelay:0.5];
}


- (void)dismissAlertView:(UIAlertView*)alert{
    [alert dismissWithClickedButtonIndex:0 animated:YES];
}
- (void)dismisAllertComplete:(UIAlertController*)alertController{
    [alertController dismissViewControllerAnimated:YES completion:nil];
}

- (void)didClick{
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(didClick)];
    self.navigationItem.rightBarButtonItem = barButton;
    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        long long cachesSize = [GetFileSize folderSizeAtPath:NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject];
        NSNumber *number = [NSNumber numberWithLongLong:cachesSize];
        [self performSelectorOnMainThread:@selector(showAlertController:) withObject:number waitUntilDone:YES];
        
    });
    
}

#pragma mark -
#pragma mark UITableView Datasource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
    
    return _titlesArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
      // cell.textLabel.textColor = [UIColor whiteColor];
      // cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
      //  cell.selectedBackgroundView = [[UIView alloc] init];
       
    }
    
    cell.selectedBackgroundView=[[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"Image-4"]];
    cell.textLabel.text = _titlesArray[indexPath.row];
    cell.textLabel.textColor=[UIColor whiteColor];
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"Image-%ld",(long)indexPath.row]];
    
    
    
    return cell;

}

@end
